//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "IdBaseComponent"
#pragma link "IdCmdTCPClient"
#pragma link "IdCmdTCPServer"
#pragma link "IdComponent"
#pragma link "IdContext"
#pragma link "IdCustomTCPServer"
#pragma link "IdTCPClient"
#pragma link "IdTCPConnection"
#pragma link "IdTCPServer"
#pragma link "IdIntercept"
#pragma link "IdInterceptSimLog"
#pragma link "IdLogBase"
#pragma link "IdLogDebug"
#pragma link "IdCompressionIntercept"
#pragma link "IdIOHandler"
#pragma link "IdIOHandlerSocket"
#pragma link "IdIOHandlerStack"
#pragma link "IdAntiFreeze"
#pragma link "IdAntiFreezeBase"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}

TIdCommandHandler *cmd_handler = NULL;

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
   //IdCmdTCPServer1->ReuseSocket = rsTrue;
//   IdCmdTCPServer1->Bindings->Add();
//   IdCmdTCPServer1->Bindings->Items[0]->IP = "127.0.0.2";
//   IdCmdTCPServer1->Bindings->Items[0]->Port = 1234;


   IdCmdTCPClient1->Host = "127.0.0.1";
   IdCmdTCPClient1->Port = 1234;
//



//  // TCollectionItem
//   IdCmdTCPClient1->CommandHandlers->Add();
//
//   IdCmdTCPClient1->CommandHandlers->Items[0]->Command = "cmd";      // = cmd_handler;
//   IdCmdTCPClient1->CommandHandlers->Items[0]->OnCommand = IdCmdTCPServer1CommandHandlers0Command;
//   IdCmdTCPClient1->CommandHandlers->Items[0]->Enabled = true;
//   IdCmdTCPClient1->CommandHandlers->Items[0]->ParseParams = true;
//   IdCmdTCPClient1->CommandHandlers->Items[0]->ParseParams = true;
//   IdCmdTCPClient1->CommandHandlers->Items[0]->Name = "handle";
//







   IdCmdTCPServer1->Active = true;
   IdCmdTCPClient1->Connect();


}

void __fastcall TForm1::IdCmdTCPServer1CommandHandlers_Command(TIdCommand *ASender)
{
   ASender->SendReply();
   Memo->Lines->Add(ASender->RawLine);
}


//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{


//   IdCmdTCPClient1->Socket->Close();
   IdCmdTCPClient1->DisconnectNotifyPeer();
//   IdCmdTCPClient1->Disconnect(true);


   IdCmdTCPServer1->Active = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::IdCmdTCPServer1BeforeCommandHandler(TIdCmdTCPServer *ASender,
          UnicodeString &AData, TIdContext *AContext)
{
   Memo->Lines->Add("Before handle Raw: " + AData);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::IdCmdTCPServer1CommandHandlers0Command(TIdCommand *ASender)

{
   UnicodeString asd = ASender->RawLine;
   if (ASender->Params->Count)
   {
       asd += "Param: ";
       for (int i=0;i<ASender->Params->Count;i++)
       {

          asd += ASender->Params->Strings[i]+", ";
       }
   }
   Memo->Lines->Add("Command handler: " + asd);


}
//---------------------------------------------------------------------------
void __fastcall TForm1::Memo1KeyDown(TObject *Sender, WORD &Key, TShiftState Shift)

{
   if (Key != VK_RETURN) return;

   int linelast = Memo1->Lines->Count-1;
   UnicodeString kkkk = Memo1->Lines->Strings[linelast];


   if (kkkk == "") return;
   if (IdCmdTCPClient1->Connected())
   {
     //IdCmdTCPClient1->SendCmd(kkkk);
      IdCmdTCPClient1->Socket->WriteLn(kkkk);
     // UnicodeString read = IdCmdTCPClient1->GetResponse();

      int asd=0;

   }


}
//---------------------------------------------------------------------------



void __fastcall TForm1::IdCmdTCPServer1Status(TObject *ASender, const TIdStatus AStatus,
          const UnicodeString AStatusText)
{
   Memo->Lines->Add("IdCmdTCPServer1Status: " + AStatusText);
}
//---------------------------------------------------------------------------

